import * as JobrService from '../services/JobrService';

export default {
  namespace: 'job',
  state: {
    list: [],
    total: null,
    page: null,
  },
  reducers: {
    save(state, { payload: { data : list, total, page } }) {
      // list.forEach(item=>{
      //   let s=new Date(item.createDatetime*1);
      //   item.createDatetime=s.toLocaleString()
      // });
      console.log(list);
      console.log(total);
      console.log(page);
      return { ...state, list , total, page };
    },
  },
  effects: {
    *fetch({ payload: { page = 1 } }, { call, put }) {
      const rs = yield call(JobrService.getList, { page });
      const { data } =rs.data;
      yield put({
        type: 'save',
        payload: {
          data:data.records,
          total:data.total,
          page:data.current,
        },
      });
    },
    *remove({ payload: id }, { call, put, select }) {
      yield call(JobrService.remove, id);
      const page = yield select(state => state.job.page);
      yield put({ type: 'fetch', payload: { page } });
    },
    *patch({ payload: { id, values } }, { call, put, select }) {     
      
      yield call(JobrService.patch, id, values);
      const page = yield select(state => state.job.page);
      yield put({ type: 'fetch', payload: { page } });
    },
    *add({ payload: {values } }, { call, put, select }) {
      yield call(JobrService.add,values);    
      const page = yield select(state => state.job.page);      
      yield put({ type: 'fetch', payload: { page } });
    }    
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/job') {
          dispatch({ type: 'fetch', payload: query });
        }

      });
    },
  },
};